﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    loadSetting();
    cn = new QTranslator(this);
    cn->load(":/translation/translation_cn.qm");
    en = new QTranslator(this);
    en->load(":/translation/translation_en.qm");
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_inputbro_pressed()
{
    QString fileName = QFileDialog::getOpenFileName(this, "Open Input Imgge",
                       QDir::currentPath(), "Images(*.jpg *.png)");
    if (QFile::exists(fileName))
    {
        ui->input->setText(fileName);
    }
}

void MainWindow::on_outputbro_pressed()
{
    QString filename = QFileDialog::getSaveFileName(this, "Select Input Imgge",
                       QDir::currentPath(), "*.jpg;;*.png");
    if(!filename.isEmpty())
    {
        ui->output->setText(filename);
        using namespace cv;
        Mat inpimg, outimg;
        inpimg = imread(ui->input->text().toStdString());
        if(ui->handle1->isChecked())
            cv::medianBlur(inpimg, outimg, 5);
        else if (ui->handle2->isChecked())
            cv::GaussianBlur(inpimg, outimg, Size(5, 5), 1.25);
        imwrite(filename.toStdString(), outimg);
        if(ui->display->isChecked())
            imshow("Output IMG", outimg);

    }
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    int result = QMessageBox::warning(this, "Exit", "Are U Sure",
                                      QMessageBox::Yes, QMessageBox::No);
    if(result == QMessageBox::Yes)
    {
        event->accept();
        saveSetting();
    }
    else
    {
        event->ignore();
    }

}

void MainWindow::changeEvent(QEvent *event)
{
    if(event->type() == QEvent::LanguageChange)
    {
        ui->retranslateUi(this);
    }
    else
    {
        QMainWindow::changeEvent(event);
    }
}

void MainWindow::loadSetting()
{
    QSettings settings("packt", "cv_qt", this);
    ui->input->setText(settings.value("input", "").toString());
    ui->output->setText(settings.value("output", "").toString());
}

void MainWindow::saveSetting()
{
    QSettings settings("packt", "cv_qt", this);
    settings.setValue("input", ui->input->text());
    settings.setValue("output", ui->output->text());
}


void MainWindow::on_actionchinaese_triggered()
{
    qApp->installTranslator(cn);

}

void MainWindow::on_actionEnglish_triggered()
{
    qApp->removeTranslator(cn);

}
